package com.vipsoft.locker.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vipsoft.base.exception.CustomException;
import com.vipsoft.base.entity.locker.LocationInfo;
import com.vipsoft.locker.mapper.LocationMapper;
import com.vipsoft.locker.service.ILocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LocationServiceImpl extends ServiceImpl<LocationMapper, LocationInfo> implements ILocationService {

    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private LocationMapper lockerRecordMapper;


    /**
     * 创建记录
     *
     * @param param
     * @return
     */
    @Override
    public boolean save(LocationInfo param) {
        boolean result = false;

        //重复检查
        LambdaQueryWrapper<LocationInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(j -> j.eq(LocationInfo::getMemberId, param.getMemberId()));
        queryWrapper.and(j -> j.eq(LocationInfo::getLocationName, param.getLocationName()));
        LocationInfo entity = this.getOne(queryWrapper,false);
        if (StrUtil.isEmpty(param.getId())) {
            if (entity != null) {
                throw new CustomException(6002, "不能重复添加数据 ");
            }
            result = super.save(param);
        } else {
            if (entity != null && !entity.getId().equals(param.getId())) {
                throw new CustomException(6001, "位置，存在重复");
            }
            result = super.updateById(param);
        }
        return result;
    }

}
